App({
  onLaunch: function () {
    // console.log('App Launch')
  },
  onShow: function () {
    // console.log('App Show')
  },
  onHide: function () {
    // console.log('App Hide')
  },
  globalData: {
    hasLogin: false,
    server: "http://www.iychua.com:8080",
    userData: '',
    imgServer: "https://kzych.oss-cn-qingdao.aliyuncs.com",
    EARTH_RADIUS: 6378137.0,   //单位M
    PI: Math.PI,

  },
  login: function (fn) {
    var self = this;
    if (!this.globalData.hasLogin) {
      //要绑定了开发者帐号才能获得openid和unionid
      wx.getUserInfo({
        success: function (res) {
          var userInfo = res.userInfo
          console.log(userInfo);
          //设置定位城市
          //self.setData({locationCity:userInfo.c});
          /*var nickName = userInfo.nickName
          var avatarUrl = userInfo.avatarUrl
          var gender = userInfo.gender //性别 0：未知、1：男、2：女
          var province = userInfo.province
          var city = userInfo.city
          var country = userInfo.country;*/
          //this.setGlobalData("userData", userInfo)
          self.globalData.userData = userInfo;
         
          //调用支持第三方登录的接口登录，注册登录一步完成 ajax
          //this.setGlobalData("hasLogin", true)
          wx.request({
            url: getApp().globalData.server + "/wx_user/login.do",
            method: 'POST',
            data: {wx_username:userInfo.nickName} ,
            header: {
              'Content-Type': 'application/json'
            },
            success: function (res) {
              if (res.data.status != 0) {
                // wx.showToast({
                //   title: res.data.msg,
                //   icon: 'none',
                //   duration: 1000,
                //   mask: true
                // })
                if (typeof (fn == "function")) {
                  fn();
                }
              } else {
                //console.log(requestObj);
                console.log(res.data);
                //状态的设置
                self.globalData.hasLogin = true;
                console.log(userInfo);
                if (typeof (fn == "function")) {
                  fn();
                }
              }

            },
            fail: function () {
              wx.showToast({
                title: "登陆失败",
                icon: 'none',
                duration: 1000,
                mask: true
              })
            }
          })


        }
      });
    } else {
      if (typeof (fn == "function")) {
        fn();
      }
    }
  }
})
